/*
 * Copyright (C) 2011 flashflexpro@gmail.com  All Rights Reserved.  No
 *   use, copying or distribution of this work may be made except in
 *   accordance with a valid license agreement from flashflexpro@gmail.com
 *   This notice must be included on all copies, modifications and
 *   derivatives of this work.
 *
 *   flashflexpro@gmail.com MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT
 *   THE SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED,
 *   INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
 *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 *   NON-INFRINGEMENT. flashflexpro@gmail.com SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED
 *   BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS
 *   SOFTWARE OR ITS DERIVATIVES.
 */

package com.flashflexpro.as3lib {
import flash.display.DisplayObject;

import mx.collections.IList;
import mx.core.Container;


/**
 * @probblem  When Group( IVisualElementContainer ) set element index,
 *  it will remove it first, and add again!
 */
public class ListToMxContainerSyncer extends ModelToViewSyncer{

    
    public function ListToMxContainerSyncer(thisObj:Object, modelList:IList, viewList:Container) {
        super(thisObj, modelList, viewList);
    }

    public function get mxContainer():Container{
        return Container(_viewList);
    }

    public function get list():IList{
        return IList(_modelList);
    }

    public function syncListToVContainer( doesViewEleMatchModelEle:Function, newViewEleFromModelEle:Function ):void{
        var getModelEleAt:Function = function( index:int ):Object{
            return list.getItemAt( index );
        };

        var getViewListLength:Function = function():int{
            return mxContainer.numChildren;
        };

        var getViewEleAt:Function = function( index:int ):Object{
            return mxContainer.getChildAt( index );
        };

        var addViewEle:Function = function( ele:DisplayObject ):void{
            mxContainer.addChild( ele );
        };

        var setViewEleIndex:Function = function( ele:DisplayObject, index:int ):void{
            mxContainer.setChildIndex( ele, index );
        };

        var removeViewEleAt:Function = function( index:int ):void{
            mxContainer.removeChildAt( index );
        };

        super.sync( list.length, getModelEleAt, getViewListLength, getViewEleAt, addViewEle, setViewEleIndex,
                removeViewEleAt, doesViewEleMatchModelEle, newViewEleFromModelEle );
    }
}
}